﻿// Taken from E1 by David Schwartz, updated by Blake Gross

package code{
	import flash.display.Shape;
	
	public class Polygon extends Shape {
		private var radius:Number;     // radius from center of polygon
		private var center:Vertex;     // center of polygon
		private var sides:Number;      // number of sides
		private var color:uint;        // color of polygon
		private var thickness:Number;  // thickness of outer line
		public var vertices:Array;    // array of polygon vertices
		private var hollow:Boolean;    // is the polygon hollow?
		public var speed:int;
		public var size:int;
		
		// make a polygon:
		public function Polygon(sides:uint, center:Vertex, speed:int, size:Number, radius:Number=40, thickness:Number=1, color:uint=0xff0000, hollow:Boolean=false) {
			this.sides = sides;
			this.center = center;
			this.speed = speed;
			this.size = size;
			this.radius = radius;
			this.color = color;
			this.thickness = thickness;
			this.hollow = hollow;
			vertices = new Array(); 
			init();
		}
		
		// create vertices, store them, and then render:
		public function init():void {
			makeVertices();
			renderVertices();
		}
		
		// calculate and store polygon vertices:
		public function makeVertices():void {
			
			// complete me by filling in vertices array with polygon vertices that you calculate procedurally
			var theta = (2 * Math.PI) / sides;  // gets the base value of theta
			
			for(var i:int  = 0; i < sides; i++)  // number of vertices is equal to the number of sides
			{
				var xPos:Number = (radius * Math.cos(i * theta)) + center.x;  // gets the position around the radius each vertex will be and translates it by the center position
				var yPos:Number = (radius * Math.sin(i * theta)) + center.y;
				var tempVert:Vertex = new Vertex(xPos * size, yPos * size, 0, 0);  // sets up a tempVertex to add to the vertices array
				vertices.push(tempVert);
			}
			
		}
		
		
		// render the polygon:
		public function renderVertices():void {
			
			// set fill and hollowness:
			graphics.beginFill(color,alpha=(hollow?0:1));
			
			// set border:
			graphics.lineStyle(thickness,color,alpha=1);
			
			// start at this vertex (x=radius,y=0 from center)
			graphics.moveTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			
			// draw line to each vertex going clockwise from starting vertex:
			for(var side:uint=1; side < sides; side++) {
				graphics.lineTo(Vertex(vertices[side]).x, Vertex(vertices[side]).y);
			}
			
			// connect the last vertex to starting vertex:
			graphics.lineTo(Vertex(vertices[0]).x, Vertex(vertices[0]).y);
			
			// done:
			graphics.endFill();
		}
		
	}
}